package com.zhangxu.microservice.auth.controller.ae;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhangxu.microservice.auth.biz.dto.SystemDTO;
import com.zhangxu.microservice.auth.biz.dto.UpdateSystemDTO;
import com.zhangxu.microservice.auth.biz.service.SystemService;
import com.zhangxu.microservice.auth.common.enums.SystemStatusEnum;
import com.zhangxu.microservice.auth.dao.condition.SystemCondition;
import com.zhangxu.microservice.auth.mapstruct.SystemWebConverter;
import com.zhangxu.microservice.auth.vo.req.CreateSystemReq;
import com.zhangxu.microservice.auth.vo.req.PageReq;
import com.zhangxu.microservice.auth.vo.req.UpdateSystemReq;
import com.zhangxu.microservice.auth.vo.resp.PageResponse;
import com.zhangxu.microservice.auth.vo.resp.SystemResp;
import com.zhangxu.microservice.common.result.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统管理控制器
 */
@Tag(name = "系统管理", description = "系统管理相关接口")
@RestController
@RequestMapping("/ae/systems")
@RequiredArgsConstructor
public class SystemController {

    private final SystemService systemService;
    private final SystemWebConverter systemWebConverter;

    @Operation(summary = "创建系统", description = "创建新系统")
    @PostMapping
    public CommonResult<Long> createSystem(@RequestBody @Validated CreateSystemReq createSystemReq) {
        Long systemId = systemService.createSystem(systemWebConverter.toCreateSystemDTO(createSystemReq));
        return CommonResult.success(systemId);
    }

    @Operation(summary = "更新系统", description = "更新现有系统信息")
    @PutMapping("/{systemId}")
    public CommonResult<Void> updateSystem(@Parameter(description = "系统ID", required = true, example = "1") @PathVariable("systemId") Long systemId,
                                           @RequestBody @Validated UpdateSystemReq updateSystemReq) {
        UpdateSystemDTO updateSystemDTO = systemWebConverter.toUpdateSystemDTO(updateSystemReq);
        updateSystemDTO.setId(systemId);
        systemService.updateSystem(updateSystemDTO);
        return CommonResult.success();
    }

    @Operation(summary = "删除系统", description = "根据ID删除系统")
    @DeleteMapping("/{systemId}")
    public CommonResult<Void> deleteSystem(
            @Parameter(description = "系统ID", required = true, example = "1")
            @PathVariable("systemId") Long systemId) {
        systemService.deleteSystem(systemId);
        return CommonResult.success();
    }

    @Operation(summary = "启用系统", description = "启用指定系统")
    @PostMapping("/{systemId}/enable")
    public CommonResult<Void> enableSystem(
            @Parameter(description = "系统ID", required = true, example = "1")
            @PathVariable("systemId") Long systemId) {
        systemService.enableSystem(systemId);
        return CommonResult.success();
    }

    @Operation(summary = "禁用系统", description = "禁用指定系统")
    @PostMapping("/{systemId}/disable")
    public CommonResult<Void> disableSystem(
            @Parameter(description = "系统ID", required = true, example = "1")
            @PathVariable("systemId") Long systemId) {
        systemService.disableSystem(systemId);
        return CommonResult.success();
    }

    @Operation(summary = "获取系统详情", description = "根据ID获取系统详情")
    @GetMapping("/{systemId}")
    public CommonResult<SystemResp> getSystemById(
            @Parameter(description = "系统ID", required = true, example = "1")
            @PathVariable("systemId") Long systemId) {
        SystemDTO systemDTO = systemService.getSystemById(systemId);
        return CommonResult.success(systemWebConverter.toSystemResp(systemDTO));
    }

    @Operation(summary = "系统列表", description = "分页查询系统列表")
    @GetMapping
    public CommonResult<PageResponse<SystemResp>> systemPage(
            @Valid PageReq pageReq,
            @Parameter(description = "系统编码") @RequestParam(value = "systemCode", required = false) String systemCode,
            @Parameter(description = "系统名称") @RequestParam(value = "name", required = false) String name,
            @Parameter(description = "系统状态") @RequestParam(value = "status", required = false) SystemStatusEnum status,
            @Parameter(description = "系统负责人用户ID") @RequestParam(value = "ownerId", required = false) Long ownerId) {
        SystemCondition condition = new SystemCondition();
        condition.setSystemCode(systemCode);
        condition.setName(name);
        condition.setStatus(status);
        condition.setOwnerId(ownerId);
        condition.setPageNum(pageReq.getPageNum());
        condition.setPageSize(pageReq.getPageSize());
        IPage<SystemDTO> page = systemService.systemPage(condition);
        return CommonResult.success(PageResponse.convert(page, systemWebConverter::toSystemResp));
    }
}